<?php
namespace backend\controllers;

use backend\helpers\commonBack;
use common\helpers\commonApi;
use common\models\Goods;
use common\models\Order;
use common\models\OrderGoods;
use common\models\WatchLog;
use common\services\ConfigService;
use common\services\MenuService;

/**
 * 首页控制器
 * Class IndexController
 * @package backend\controllers
 */
class IndexController extends BaseController
{
    public function actionIndex(){
        $menuHtml=MenuService::getMenuListByLogin();
        $adminInfo=commonBack::adminLoginInfo();

        unset($adminInfo['menu']);
        unset($adminInfo['role']);
        $mame = ConfigService::getConfig('sys_config_sysname');
        $record_no = ConfigService::getConfig('sys_config_record_no');
        return $this->renderPartial('index',['menuHtml'=>$menuHtml,'adminInfo'=>$adminInfo,'mame'=>$mame,'record_no'=>$record_no]);
    }

    public function actionHome(){
//成交订单
        $order_num = ['day'=>0,'yester'=>0,'diff'=>0,'total'=>0,];
        //营业额
        $order_amount = ['day'=>0,'yester'=>0,'diff'=>0,'total'=>0,];
        //进店人数
        $enter_num = ['day'=>0,'yester'=>0,'diff'=>0,'total'=>0,];

        //今日数据
        $day_start = date('Y-m-d 00:00:00');
        $day_end = date('Y-m-d 23:59:59');
        $day = Order::getMarketData($day_start,$day_end,'');
        $order_num['day'] = $day['number'];
        $order_amount['day'] = $day['order_amount'];
        //昨日数据
        $yester_start = date("Y-m-d 00:00:00",strtotime("-1 day"));
        $yester_end = date("Y-m-d 23:59:59",strtotime("-1 day"));
        $yester = Order::getMarketData($yester_start,$yester_end,'');
        $order_num['yester'] = $yester['number'];
        $order_amount['yester'] = $yester['order_amount'];
        //累计数据
        $total = Order::getMarketData('','','');
        $order_num['total'] = $total['number'];
        $order_amount['total'] = $total['order_amount'];
        //差异
        $order_num['diff'] = $this->compare($order_num['day'],$order_num['yester']);
        $order_amount['diff'] = $this->compare($order_amount['day'],$order_amount['yester']);
        $enter_num['diff'] = $this->compare($enter_num['day'],$enter_num['yester']);

        //销冠商品（前5名）
        $ranking = OrderGoods::find()->alias('og')
            ->leftJoin(Order::tableName().' as o','o.id=og.order_id')
            ->leftJoin(Goods::tableName().' as g','g.id=og.good_id')
            ->where(['o.pay_status'=>[1,2,3]])
            ->select(['sum(og.total_pay_price) as total_pay_price','g.name'])
            ->groupBy(['og.good_id'])
            ->orderBy(['total_pay_price'=>SORT_DESC])
            ->limit(5)
            ->asArray()->all();
        return $this->render('',[
            'ranking'=>$ranking,
            'order_num'=>$order_num,
            'order_amount'=>$order_amount,
            'enter_num'=>$enter_num,
        ]);
    }



    public function actionDownload(){
        $fileName = \Yii::$app->request->get('fileName','');
        if(!$fileName) return $this->tError('参数错误');
        $root =\Yii::getAlias('@approot');
        if(!file_exists($root.$fileName)) return $this->tError('文件不存在');
        $fileUrl = commonApi::getDomain($fileName);
        header('location:'.$fileUrl);
    }

    /**
     * 比较大小
     * @param $day
     * @param $yes
     * @return string
     */
    public function compare($day,$yes)
    {
        $diff = $day-$yes;
        if ($diff>0){
            return '涨：<span class="text-style">'.$diff.'</span>';
        }elseif ($diff==0){
            return '平';
        }else{
            return '跌：<span class="text-style">'.abs($diff).'</span>';
        }
        return '';
    }
}
